using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccess
{
    public partial class RoleDao : IDao<Role>
    {
        public Role Select(Guid id)
        {
            Role role = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_Role_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        role = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return role;
        }

        public void Insert(Role role)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_Role_Insert";
            command.Parameters.Add(new SqlParameter("@Id", role.Id));
            command.Parameters.Add(new SqlParameter("@SystemId", role.SystemId));
            command.Parameters.Add(new SqlParameter("@RoleName", role.RoleName));
            command.Parameters.Add(new SqlParameter("@Description", role.Description));
            command.Parameters.Add(new SqlParameter("@IsActive", role.IsActive));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(Role role)
        {
            if (role.UpdateTimestamp == DateTime.MinValue) role.UpdateTimestamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_Role_Update";
            command.Parameters.Add(new SqlParameter("@Id", role.Id));
            command.Parameters.Add(new SqlParameter("@SystemId", role.SystemId));
            command.Parameters.Add(new SqlParameter("@RoleName", role.RoleName));
            command.Parameters.Add(new SqlParameter("@Description", role.Description));
            command.Parameters.Add(new SqlParameter("@IsActive", role.IsActive));
            command.Parameters.Add(new SqlParameter("@UpdateTimestamp", DateTime.Now));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_Role_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Associated Entity Permission Related

        const string Permission_ColumnName = "RoleId";
        const string Permission_TableName = "PR_Role_Permission";
        const string Permission_KeyColumnName = "PageId";
        const string Permission_ValueColumnName = "PermissionId";

        public void FillPermissions(Role role)
        {
            DaoHelper.FillEntity<Guid, Guid>(role.Permissions, role.Id, Permission_ColumnName, Permission_TableName, Permission_KeyColumnName, Permission_ValueColumnName);
        }

        public void SavePermissions(Role role)
        {
            TransactionHelper.ProcessTransaction
            (() =>
                {
                    DaoHelper.RemoveEntity(role.Id, Permission_ColumnName, Permission_TableName);
                    DaoHelper.InsertEntity<Guid, Guid>(role.Permissions, role.Id, Permission_ColumnName, Permission_TableName, Permission_KeyColumnName, Permission_ValueColumnName);
                }
            );
        }

        #endregion

        #region Private Method

        internal static Role CreateEntityByReader(IDataReader reader)
        {
            Role role = new Role();
            if (reader["Id"] != DBNull.Value) role.Id = (Guid)reader["Id"];
            if (reader["SystemId"] != DBNull.Value) role.SystemId = (Guid)reader["SystemId"];
            if (reader["RoleName"] != DBNull.Value) role.RoleName = reader["RoleName"] as string;
            if (reader["Description"] != DBNull.Value) role.Description = reader["Description"] as string;
            if (reader["IsActive"] != DBNull.Value) role.IsActive = (Boolean)reader["IsActive"];
            if (reader["InsertTimestamp"] != DBNull.Value) role.InsertTimestamp = (DateTime)reader["InsertTimestamp"];
            if (reader["UpdateTimestamp"] != DBNull.Value) role.UpdateTimestamp = (DateTime)reader["UpdateTimestamp"];

            return role;
        }

        #endregion
    }
}